package com.innoking.docsys.user.service.impl;

import com.innoking.docsys.user.dao.cluster.ClusterUpmsRoleDao;
import com.innoking.docsys.user.dto.UpmsRole;
import com.innoking.docsys.user.dto.UpmsUser;
import com.innoking.docsys.user.module.UpmsRoleVO;
import com.innoking.docsys.user.module.UpmsUserRoleObjVO;
import com.innoking.docsys.user.service.UserRoleRelationService;
import com.innoking.docsys.user.service.UserService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class UserRoleRelationServiceIMPL implements UserRoleRelationService {

    @Autowired
    UserService userService;

    @Autowired
    ClusterUpmsRoleDao clusterUpmsRoleDao;

    /**
     * 获取用户角色权限的集合
     * @param userId
     * @return sunq
     */
    @Override
    public UpmsUserRoleObjVO getUserRoleList(Integer userId) {

        UpmsUserRoleObjVO objVO=new UpmsUserRoleObjVO();
        objVO.setUserId(userId);
        UpmsUser user=userService.getUserbyId(userId);
        objVO.setUsername(user.getUsername());

        List<UpmsRole> roleList=clusterUpmsRoleDao.getUserRoleObj(userId);
        if(roleList==null){
            objVO.setRoleList(new ArrayList<>());
            return  objVO;
        }
        List<UpmsRoleVO> voList= new ArrayList<>();
        roleList.stream().forEach(t-> {UpmsRoleVO temp=new UpmsRoleVO();
                                        BeanUtils.copyProperties(t,temp);
                                        voList.add(temp);
                                        });
        objVO.setRoleList(voList);
        voList.stream().forEach(t->{
                                       if( t.getDocMgr()>0 ) {
                                           objVO.setDocMgr(t.getDocMgr());
                                       }
                                        if( t.getDocEdit()>0 ) {
                                            objVO.setDocEdit(t.getDocEdit());
                                        }
                                        if( t.getDocLevel()>objVO.getDocLevel() ) {
                                            objVO.setDocLevel(t.getDocLevel());
                                        }
                                   }
                                 );

        return objVO;
    }
}